package com.pet.server.ext.util;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;

import com.pet.server.ext.data.UserInfo;


public class CookieHelper {

    public static final String FIELD_NAME = "petuserpass";
    public static final String KEY = "dfyfhqs9";
    
    public static void removeCookie(){
    	
    }
    
    
    public static UserInfo getUserInfo(String cookie){
    	UserInfo mobile = new UserInfo();
    	if (cookie != null) {
    	    try{
        	    byte[] cookieBytes = ByteHelper.hex2byte(cookie);
                byte[] decodedBytes = decryptData(cookieBytes, KEY.getBytes());
            	String decodedString = new String(decodedBytes);
            	System.out.println(decodedString+" =================");
            	//String[] parts = decodedString.split("@");
            	String[] parts = decodedString.split(",");

            	mobile.user_id = Integer.parseInt(parts[0]);
            	mobile.user_name=parts[1];
                mobile.user_type=parts[2];
                mobile.nick_name=parts[3];
                mobile.ico_path=StringUtil.isEmpty(parts[4])?null:parts[4];
                //System.out.println("==================================="+mobile.user_id);
    	    }catch(Exception e){    		
    	    	e.printStackTrace();
    	    }
    	}
    	//System.out.println(mobile.user_id);
        return mobile;
    }
    
    public static String createCookie(UserInfo mobile){
    	String cookie = null;
    	try{
        	//String content = mobile.user_id + "@"+mobile.user_name+ "@" + mobile.user_type + "@" + mobile.nick_name +"@"+StringUtil.dealNull(mobile.ico_path)+"@"+ (System.currentTimeMillis() / 1000);
        	String content = mobile.user_id + ","+mobile.user_name+ "," + mobile.user_type + "," + mobile.nick_name +","+StringUtil.dealNull(mobile.ico_path)+","+ (System.currentTimeMillis() / 1000);
        	byte[] contentBytes = content.getBytes();
        	byte[] keyBytes = KEY.getBytes();
            byte[] cookieBytes = encryptData(contentBytes, keyBytes);
            cookie = ByteHelper.byte2hex(cookieBytes);
    	}catch(Exception e){    		
    	    e.printStackTrace();
    	}
        return cookie;
    } 
    
    
   

    public static byte[] encryptData(byte[] input, byte[] key) throws Exception { 
        SecretKey deskey = new javax.crypto.spec.SecretKeySpec(key, ALGORITHM); 
        Cipher c1 = Cipher.getInstance(ALGORITHM); 
        c1.init(Cipher.ENCRYPT_MODE, deskey); 
        byte[] cipherByte = c1.doFinal(input);   
        return cipherByte;  
    } 
    
    public static byte[] decryptData(byte[] input, byte[] key) throws Exception { 
        SecretKey deskey = new javax.crypto.spec.SecretKeySpec(key, ALGORITHM); 
        Cipher c1 = Cipher.getInstance(ALGORITHM); 
        c1.init(Cipher.DECRYPT_MODE, deskey); 
        byte[] clearByte = c1.doFinal(input);   
        return clearByte; 
    } 
    
    public static final String ALGORITHM = "DES";
    
    public static void main(String[] args) {
    	
    	getUserInfo("0336D274951CE88ADE4896D4C313690AE2BC58FF7144DAFF53AA20C0F1CD82E5");
    	
    	/*
        Account account = new Account();
        account.accountId = 8;
        account.shopId = 5;
        String cookie = createCookie(account);
        System.out.println(cookie);
        account = getAccount("DE14C3B2C5281A40DD8CDBC3EFEFDA771A1382EF46E6280A");
        System.out.println(account.accountId);*/
        
    }
}
